﻿using UnityEngine;
using System.Collections;
using YanhuaMMO;
using System;
using SimpleJSON;
public class CameraMain : MonoBehaviour {

    public Transform target;
    public Vector3 offset;
    private Vector3 min;
    private Vector3 max;
    public float smoothing = 5f;

    public bool isZoomEffect = false;

    private Transform zoomtarget;

    private float zoomtime = 1;

    private float shake = 0f;

    // 抖动幅度（振幅）
    //振幅越大抖动越厉害
    private float shakeAmount = 0.3f;
    private float decreaseFactor = 1.0f;

    private float movedist = 5;

    private void Awake()
    {
        shakeAmount = 0.5f;

        initMoveFoward();

     //   InvokeRepeating("gccollect", 5, 10);
    }

    private void OnEnable()
    {
        this.enabled = true;
    }
    private void gccollect()
    {
        Resources.UnloadUnusedAssets();
        GC.Collect();
    }
    public void SetMapSize(float left,float right)
    {
        //string[] sizeArr = sizeStr.Split(',');
        min = new Vector3(left, 12.5f, -18f);
        max = new Vector3(right, 12.5f, -18f);
    }

    IEnumerator initMoveFoward()
    {
        object[] globalcfg = Util.CallMethod("OffLine", "loadData", "Global");

        while (globalcfg == null || globalcfg.Length <= 0)
        {
            yield return 0;
            globalcfg = Util.CallMethod("OffLine", "loadData", "Global");
        }

        String jsonStr = globalcfg[0] as String;

        JSONNode globalConfig = JSON.Parse(jsonStr);
        foreach (var item in globalConfig.Values)
        {
            if (item["ID"] == 109)
            {
                movedist = int.Parse(item["FunParameter"]);
            }
        }
    }
    private void Update()
    {
      
        if (shake > 0)
        {
            transform.localPosition = transform.localPosition + UnityEngine.Random.insideUnitSphere * shakeAmount;

            shake -= Time.deltaTime * decreaseFactor;
        }
        else
        {
            shake = 0f;
           // transform.localPosition = transform;
        }

    }
    // Update is called once per frame
    void LateUpdate()
    {
        if (isZoomEffect)
            return;
        if (null== target)
        {
            var player = GameObject.FindWithTag("Player");
            if (null != player)
            {
                target = player.transform;
            }
            
        }
        if (null != target)
        {
            Vector3 goalPoint = target.position + offset;
            //goalPoint.x = Mathf.Clamp(goalPoint.x, min.x, max.x);
            goalPoint.y = 9f;// Mathf.Clamp(goalPoint.y, min.y, max.y);
            goalPoint.z = Mathf.Clamp(goalPoint.z, min.z, max.z);

            //transform.position = Vector3.Lerp(transform.position, goalPoint, smoothing * Time.deltaTime);
        }
        
    }

    public void startShake(float shaketime,float delaytime)
    {
        StartCoroutine(delayshake(shaketime,delaytime));
    }

    IEnumerator delayshake(float shaketime,float delaytime)
    {
        yield return new WaitForSeconds(delaytime / 1000);
        shake = shaketime/1000;

    }

    public void OnDisable()
    {
        StopAllCoroutines();
        isZoomEffect = false;
    }
    public void startZoomIn(Transform targetPlyer,float delaytime)
    {
        if (isZoomEffect)
            return;
        isZoomEffect = true;
        Vector3 goalPoint = targetPlyer.position;
        goalPoint.x = Mathf.Clamp(goalPoint.x, min.x, max.x);
        goalPoint.y = Mathf.Clamp(goalPoint.y, min.y, max.y);
        goalPoint.z = Mathf.Clamp(goalPoint.z, min.z, max.z);

        StartCoroutine(moveToTarget(goalPoint, delaytime));
    }

    IEnumerator moveToTarget(Vector3 goalPoint,float movetime)
    {
        yield return new WaitForSeconds(movetime / 1000);
        float times = Time.deltaTime;
        transform.position = Vector3.Lerp(transform.position, goalPoint,  times);

        while (Vector3.Distance(transform.position,goalPoint) > 1)
        {
            yield return 0;
            times += Time.deltaTime;
            transform.position = Vector3.Lerp(transform.position, goalPoint,  times);
        }

        transform.position = goalPoint;
       yield return new WaitForSeconds(0.5f);

        float hasmove = 0;
        int movestep = (int)(Mathf.Floor(movedist / 0.2f));
        while (hasmove < movestep)
        {
            yield return 0;
            hasmove++;
            transform.localPosition += transform.forward * 0.2f;
        }

        yield return new WaitForSeconds(0.5f);
        movestep = (int)(Mathf.Floor(movedist / 0.8f));
        hasmove = 0;
        while (hasmove < movestep)
        {
            yield return 0;
            hasmove++;
            transform.localPosition -= transform.forward * 0.9f;
        }

        isZoomEffect = false;
    }
}
